package com.victor.handler;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.victor.utils.Msg;

/**
 * 使用@ControllerAdvice来进行统一异常处理
 * 
 * @author zhangbin
 *
 */
//@ControllerAdvice
public class ExceptionHandler {

//	@org.springframework.web.bind.annotation.ExceptionHandler(value = Exception.class)
//	@ResponseBody
	public Msg handler(Exception e) {
		if (e instanceof NullPointerException) {
			return Msg.fail("空指针异常");
		} else if (e instanceof SQLException) {
			return Msg.fail("数据库访问异常");
		} else if (e instanceof IndexOutOfBoundsException) {
			return Msg.fail("数组越界异常");
		} else if (e instanceof NumberFormatException) {
			return Msg.fail("字符串转换异常");
		} else if (e instanceof FileNotFoundException) {
			return Msg.fail("文件异常");
		} else if (e instanceof IOException) {
			return Msg.fail("IO异常");
		} else if (e instanceof ClassCastException) {
			return Msg.fail("类型转换异常");
		} else if (e instanceof ArrayStoreException) {
			return Msg.fail("数组存放异常");
		} else if (e instanceof IllegalArgumentException) {
			return Msg.fail("参数错误异常");
		} else if (e instanceof ArithmeticException) {
			return Msg.fail("运算条件异常");
		} else if (e instanceof NegativeArraySizeException) {
			return Msg.fail("数组创建异常");
		} else if (e instanceof NoSuchMethodException) {
			return Msg.fail("方法丢失异常");
		} else if (e instanceof SecurityException) {
			return Msg.fail("安全管理器异常");
		} else if (e instanceof UnsupportedOperationException) {
			return Msg.fail("不支持的请求操作异常");
		} else if (e instanceof RuntimeException) {
			return Msg.fail("java运行异常");
		} else if (e instanceof NoHandlerFoundException) {
			return Msg.fail("404--请求路径错误");
		} else {// 其他未捕获异常
			return Msg.fail("服务器内部错误,请联系系统管理员!");
		}
	}

}